import { UIType } from "../Enum";
import UIManager from "../UIManager";
import DataStorage from "../utils/DataStorage";
import Util from "../utils/Util";
import UIBase from "./UIBase";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LevelSelect extends UIBase {

    @property(cc.Node)
    backStartButton: cc.Node = null

    @property(cc.Node)
    levelsRoot: cc.Node = null

    private _uiManager: UIManager = null

    onLoad() {
        super.onLoad()
    }

    start() {

    }

    public show(): void {
        super.show()
        // 显示解锁
        this.levelsRoot.children.forEach((node, index) => {
            const lableNode = node.children[1]
            // 解锁数字
            const unLockLevel = DataStorage.getUnLockLevel()
            const level = unLockLevel
            const labelComp = lableNode.getComponent(cc.Label)
            labelComp.string = index + 1 <= level ? '已解锁' : '未解锁'
        })
    }

    public init(uiManager: UIManager) {
        this._uiManager = uiManager
        const { TOUCH_START, TOUCH_END, TOUCH_CANCEL } = cc.Node.EventType
        // 按键事件
        // 开始按钮
        this.backStartButton.on(TOUCH_START, () => {
            Util.clickDownTween(this.backStartButton)
        })
        this.backStartButton.on(TOUCH_END, () => {
            Util.clickUpTween(this.backStartButton, () => {
                this._uiManager.toStartMenu()
            })
        })
        this.backStartButton.on(TOUCH_CANCEL, () => {
            Util.clickUpTween(this.backStartButton)
        })

        // 关卡列表
        this.levelsRoot.children.forEach((node, index) => {
            const button = node.children[0]
            button.on(TOUCH_START, () => {
                Util.clickDownTween(button)
            })
            button.on(TOUCH_END, () => {
                Util.clickUpTween(button, () => {
                    const level = index + 1
                    console.log(`level:`, level)
                    // 显示游戏界面
                    const unLockLevel = DataStorage.getUnLockLevel()
                    // 已解锁才可以进入游戏
                    if (unLockLevel >= level) {
                        this._uiManager.startGame(level)
                    }
                })
            })
            button.on(TOUCH_CANCEL, () => {
                Util.clickUpTween(button)
            })
        })
    }
}
